﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NFramework.UnitTests.Properties;
using NFramework.DataAccess;

namespace NFramework.UnitTests
{
    /// <summary>
    /// Summary description for Global
    /// </summary>
    [TestClass]
    public class Global
    {
        public Global() { }

        private static string ConnectionStringForMaster
        {
            get
            {
                if (_connectionStringWithoutDatabase == null) {
                    _connectionStringWithoutDatabase =
                        string.Format("Data Source={0};Initial Catalog=master;User ID={1};Password={2};",
                        Settings.Default.DatabaseServer, Settings.Default.DatabaseUser, Settings.Default.DatabasePassword);
                }
                return _connectionStringWithoutDatabase;
            }
        }
        private static string _connectionStringWithoutDatabase;

        public static string ConnectionString
        {
            get
            {
                if (_connectionString == null)
                {
                    _connectionString = 
                        string.Format("Data Source={0};Initial Catalog={3};User ID={1};Password={2};",
                        Settings.Default.DatabaseServer, Settings.Default.DatabaseUser, Settings.Default.DatabasePassword, Settings.Default.DatabaseName);
                }
                return _connectionString;
            }
        }
        private static string _connectionString;

        [AssemblyInitialize]
        public static void RefreshDatabase(TestContext testContext)
        {
            using (DataAccessContext.PushSQL(ConnectionStringForMaster))
            {
                using (IExecutionHelper eh = ExecutionHelper.Create(string.Format(
                    @"If Exists(Select Top 1 * From sys.databases Where Name = '{0}') Drop Database {0}", 
                    Settings.Default.DatabaseName)))
                {
                    eh.Execute();
                }
                using (ExecutionHelper eh = ExecutionHelper.Create("Create Database " + Settings.Default.DatabaseName))
                {
                    eh.Execute();
                }
            }
        }

        public static void CallEmbededResourceOnDatabase(string resourceName)
        {
            using (DataAccessContext.PushSQL(ConnectionString))
            {
                using (IExecutionHelper eh = ExecutionHelper.Create(GetEmbeddedResource(resourceName)))
                {
                    eh.Execute();
                }
            }
        }

        private static string GetEmbeddedResource(string resourceName)
        {
            var resource = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
            var blah = new System.IO.StreamReader(resource);
            var result = blah.ReadToEnd();
            return result;
        }

        [TestInitialize()]
        public void MyTestInitialize() 
        {
            NFramework.DataAccess.DataAccessContext.PushSQL(ConnectionString);
            OnTestInitializing();
        }

        protected virtual void OnTestInitializing() { }

    }
}
